package com.oocl.chatroom.client.ui;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.oocl.chatroom.client.Client;
import com.oocl.chatroom.client.listener.ReceiverListener;
import com.oocl.chatroom.client.sender.ClientSender;
import com.oocl.chatroom.network.protocol.Message;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import java.awt.Color;

public class Login extends JFrame {

	private Client client;
	private Client lgclient;
	private JPanel contentPane;
	private JTextField usrname;
	private JPasswordField usrpwd;
	private JLabel nameLabel;
	private JLabel pwdLabel;
	private JButton btnLogin;
	private JButton btnReg;
	private JLabel lbStatus;

	private PrintWriter out;

	public Login() {
		this.setSize(400, 300);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(null);

		usrname = new JTextField();
		usrname.setBounds(141, 84, 226, 32);
		contentPane.add(usrname);
		usrname.setColumns(10);

		usrpwd = new JPasswordField();
		usrpwd.setBounds(141, 143, 226, 32);
		contentPane.add(usrpwd);

		nameLabel = new JLabel("UserName");
		nameLabel.setFont(new Font("Tahoma", Font.BOLD, 15));
		nameLabel.setBounds(32, 82, 94, 32);
		contentPane.add(nameLabel);

		pwdLabel = new JLabel("Password");
		pwdLabel.setFont(new Font("Tahoma", Font.BOLD, 15));
		pwdLabel.setBounds(32, 141, 94, 32);
		contentPane.add(pwdLabel);

		btnLogin = new JButton("Login");
		btnLogin.setFont(new Font("Tahoma", Font.BOLD, 14));
		btnLogin.setBounds(64, 210, 94, 32);
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String name = usrname.getText();
				String pwd = ";"+usrpwd.getText();
				
				//Access the entry server first and then access the chat server
				try {
					//Connect to the entry server, input the ip of the server
					Socket logsoc=new Socket("127.0.0.1",8889);
					client=new Client(logsoc);
					//Send msg to entry server
					Message logmsg=new Message(name+pwd,5,"","",System.currentTimeMillis());
					send(logmsg,client);
					//Receive msg from entry server
					if(new ReceiverListener(client).entryMsg()){
						//Judge the server and open the login ui,dispose the register ui
						System.out.println("login");
						lbStatus.setText("Login success!");
						Socket socket = new Socket("127.0.0.1", 8888);
						//Send online msg to server
						Message msg = new Message("", 2, name, "", System
								.currentTimeMillis());
						lgclient=new Client(socket);
						send(msg,lgclient);
						lgclient.setName(name);
						Login.this.dispose();// Close current window
						new Thread(new ReceiverListener(lgclient)).start();
					}else{
						lbStatus.setText("Please check the username/password.");
					}
				} catch (UnknownHostException e1) {
					System.out.println("connection fail");
				} catch (IOException e1) {
					System.out.println("connection fail");
				}
			}
		});
		contentPane.add(btnLogin);

		btnReg = new JButton("Sign up");
		btnReg.setFont(new Font("Tahoma", Font.BOLD, 14));
		btnReg.setBounds(273, 210, 94, 32);
		btnReg.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				Register r=new Register();
				r.setVisible(true);
				Login.this.dispose();// close current window
			}
		});
		this.contentPane.add(btnReg);
		this.setContentPane(contentPane);
		
		lbStatus = new JLabel("");
		lbStatus.setFont(new Font("Trebuchet MS", Font.BOLD, 18));
		lbStatus.setForeground(new Color(204, 0, 51));
		lbStatus.setHorizontalAlignment(SwingConstants.CENTER);
		lbStatus.setBounds(32, 31, 335, 31);
		contentPane.add(lbStatus);

	}

	private void send(Message msg,Client client) throws IOException {
		ClientSender sender = new ClientSender(client.getOut());
		sender.send(msg);
	}
	
}
